package cn.charmaine.pet.controller;

import cn.charmaine.basic.util.LoginContext;
import cn.charmaine.org.domain.Employee;
import cn.charmaine.org.domain.Logininfo;
import cn.charmaine.org.domain.User;
import cn.charmaine.org.mapper.IEmployeeMapper;
import cn.charmaine.org.mapper.UserMapper;
import cn.charmaine.pet.domain.dto.AcceptDto;
import cn.charmaine.pet.service.ISearchMasterMsgService;
import cn.charmaine.pet.domain.SearchMasterMsg;
import cn.charmaine.pet.query.SearchMasterMsgQuery;
import cn.charmaine.basic.util.AjaxResult;
import cn.charmaine.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IEmployeeMapper employeeMapper;

    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null){
                    searchMasterMsgService.update(searchMasterMsg);
            }else{
                searchMasterMsgService.add(searchMasterMsg);
            }
            return AjaxResult.thisSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.thisSuccess().setSuccess(false).setMessage("操作失败");
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.deleteById(id);
            return AjaxResult.thisSuccess();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.thisSuccess().setSuccess(false).setMessage("操作失败");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryData(query);
    }

    /**
     * 寻主发布
     * @param smm
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg smm, HttpServletRequest request){
        try {
            //用工具类取出logininfo信息
            String username = LoginContext.getCurrentUser(request).getUsername();
            User user = userMapper.selectByUsername(username);
            //将用户的id获取并设置到SearchMasterMsg中
            smm.setUserId(user.getId());
            searchMasterMsgService.publish(smm);
            return AjaxResult.thisSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.thisSuccess().setSuccess(false).setMessage(e.getMessage());
        }
    }

    /**
     * 个人中心，我发布的寻主消息
     * @param query
     * @return
     */
    @PostMapping("/posted")
    public PageList<SearchMasterMsg> posted(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request)
    {
        //用工具类得到登录信息
        Logininfo logininfo = LoginContext.getCurrentUser(request);
        //用登录信息的用户名查询用户
        User user = userMapper.selectByUsername(logininfo.getUsername());
        //将userId设置到寻主消息中
        query.setUserId(user.getId());
        return searchMasterMsgService.queryData(query);
    }

    /**
     * 后端审核未通过
     * @param query
     * @return
     */
    @PostMapping("/noPass")
    public PageList<SearchMasterMsg> noPass(@RequestBody SearchMasterMsgQuery query)
    {
        //设置审核状态为-1（驳回）
        query.setState(-1);
        return searchMasterMsgService.queryData(query);
    }

    /**
     * 寻主池消息
     * @param query
     * @return
     */
    @PostMapping("/pool")
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query)
    {
        //审核通过，但距离大于50km或者被店铺拒绝
        query.setState(1);
        return searchMasterMsgService.queryData(query);
    }

    /**
     * 店铺未审核(state=1百度ai审核通过，且有shopId)
     * @param query
     * @return
     */
    @PostMapping("/shopHandler")
    public PageList<SearchMasterMsg> shopHandler(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request)
    {
        Logininfo logininfo = LoginContext.getCurrentUser(request);
        Employee employee = employeeMapper.selectByLogininfoId(logininfo.getId());
        query.setShopId(employee.getShopId());
        query.setState(1);
        return searchMasterMsgService.queryData(query);
    }

    /**
     * 已完成
     * @param query
     * @return
     */
    @PostMapping("/complete")
    public PageList<SearchMasterMsg> complete(@RequestBody SearchMasterMsgQuery query)
    {
        //设置审核状态为已完成
        query.setState(2);
        return searchMasterMsgService.queryData(query);
    }

    /**
     * 接单
     * @param smm
     * @param request
     * @return
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody AcceptDto dto){
        try {
            searchMasterMsgService.accept(dto);
            return AjaxResult.thisSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.thisSuccess().setSuccess(false).setMessage(e.getMessage());
        }
    }
}
